package com.szsfm.vehiclemanagement.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.orhanobut.logger.Logger;
import com.szsfm.vehiclemanagement.R;
import com.szsfm.vehiclemanagement.base.BaseFragment;
import com.szsfm.vehiclemanagement.bean.Vehicle;
import com.szsfm.vehiclemanagement.event.CommEvent;
import com.szsfm.vehiclemanagement.manager.BDLocationManager;
import com.szsfm.vehiclemanagement.manager.TerminalManager;
import com.szsfm.vehiclemanagement.tcp.TcpCore;
import com.szsfm.vehiclemanagement.util.DateUtil;
import com.szsfm.vehiclemanagement.util.ToastUtil;
import com.szsfm.vehiclemanagement.view.VehicleInfoView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;


/**
 * Created by zhoubigbo on 2017/11/8.
 */
public class VehicleFragment extends BaseFragment implements View.OnClickListener {

    VehicleInfoView mVehicleInfoView;
    Button mPrintButton, mSubmitButton;
    long mPreClickMillis;
    EditText mSearchEditText;
    StringBuilder mStringBuilder = new StringBuilder();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View contentView = inflater.inflate(R.layout.fragment_vehicle, container, false);
        mVehicleInfoView = contentView.findViewById(R.id.vehicle_info);

        mPrintButton = contentView.findViewById(R.id.print_button);
        mPrintButton.setOnClickListener(this);
        mSearchEditText = contentView.findViewById(R.id.search_edt);
        mSearchEditText.setText("粤AJ5486");
        mSubmitButton = contentView.findViewById(R.id.submit_button);
        mSubmitButton.setOnClickListener(this);
        return contentView;
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventAction(CommEvent ev) {
        if (ev.type == CommEvent.EVENT_TYPE_VEHICLE) {
            if (ev.ret == 0) {
                mVehicleInfoView.clearText();
                List<Vehicle> lst = (List<Vehicle>) ev.obj;
                mVehicleInfoView.addText("车牌号:" + lst.get(0).getPlate());
                mVehicleInfoView.addText("违章次数:" + lst.size() + "\n");
                mVehicleInfoView.addText("   ");
                for (Vehicle v : lst) {
                    mVehicleInfoView.addText("违章时间:" + DateUtil.fomate2(v.getTime()));
                    if (TextUtils.isEmpty(v.getPlace())){
                        BDLocationManager.getInstance().latlng2Address(v.getLat(), v.getLng(), new BDLocationManager.ReverseAddressListener() {
                            @Override
                            public void callback(String address) {
                                mVehicleInfoView.addText("违章地点:"+address);
                            }
                        });
                    }else {
                        mVehicleInfoView.addText("违章地点:"+v.getPlace());
                    }
                    mVehicleInfoView.addText("违章类型:" + descripterOftype(v.getType()) + "\n");
                    Logger.d("vehicle=" + v.toString());
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        if (v == mPrintButton) {
            long currentMillis = System.currentTimeMillis();
            long distanceMillis = currentMillis - mPreClickMillis;
            if (distanceMillis < 3 * 1000) {
                ToastUtil.show("请勿过快打印");
                return;
            }
            mPreClickMillis = currentMillis;
            TerminalManager.get().printText(mStringBuilder.toString());
        } else if (v == mSubmitButton) {
            String txt = mSearchEditText.getText().toString().trim();
            if (TextUtils.isEmpty(txt)) {
                ToastUtil.show("请输入车牌号");
                return;
            }
            if (txt.length() < 7 || txt.length() > 10) {
                ToastUtil.show("车牌号格式不正确");
                return;
            }
            TcpCore.queryVehicleInfo(txt);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) EventBus.getDefault().register(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        if (!EventBus.getDefault().isRegistered(this)) EventBus.getDefault().register(this);
    }

    public String descripterOftype(String type) {
        if (type.equals("0")) {
            return "违章停车";
        } else if (type.equals("1")) {
            return "违章开车";
        } else if (type.equals("2")) {
            return "乱鸣喇叭";
        } else {
            return "未确定";
        }
    }

    void f(){
        mStringBuilder.append("           违停通知单\n");
        mStringBuilder.append("\n");
        mStringBuilder.append("\n");
        mStringBuilder.append("车牌:粤AJ5486\n");
        mStringBuilder.append("\n");
        mStringBuilder.append("违章时间:2017-11-28 12:26:45\n");
        mStringBuilder.append("\n");
        mStringBuilder.append("违章次数: 1\n");
        mStringBuilder.append("\n");
        mStringBuilder.append("违章地点: 深圳市金地工业区\n");
        mStringBuilder.append("\n");
        mStringBuilder.append("违章类型: 未按规定缴纳停车费用\n");
        mStringBuilder.append("\n");
        mStringBuilder.append("执法单位: 深圳市福田区xx执法大队\n");
        mStringBuilder.append("执法大队电话: 0755-83709783\n");
        mStringBuilder.append("\n");
        mStringBuilder.append("\n");
        mStringBuilder.append("\n");
        mStringBuilder.append("\n");
        mStringBuilder.append("\n");

    }

}
